package com.cyl.game.demos.web.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import java.util.List;
import com.cyl.game.demos.web.entity.Friendship;

@Mapper
public interface FriendshipMapper {
    
    // 插入一条双向好友关系记录（要确保数据一致性，插入时处理好两边关系）
    @Insert("INSERT INTO friendships (user_id_1, user_id_2) VALUES (#{user_id_1}, #{user_id_2})")
    int insertFriendship(Friendship friendship);

    // 根据友谊关系 ID 查询特定的好友关系记录
    @Select("SELECT * FROM friendships WHERE friendship_id = #{friendship_id}")
    Friendship selectFriendshipById(Integer friendship_id);

    // 根据用户 ID 查询该用户参与的所有好友关系记录（正常状态的好友）
    @Select("SELECT * FROM friendships WHERE user_id_1 = #{user_id} OR user_id_2 = #{user_id} AND status = 1")
    List<Friendship> selectFriendshipsByUserId(Integer user_id);

    // 根据两个用户 ID 查询特定的双向好友关系记录（用于判断两人是否为双向好友等情况）
    @Select("SELECT * FROM friendships WHERE (user_id_1 = #{user_id_1} AND user_id_2 = #{user_id_2}) OR (user_id_1 = #{user_id_2} AND user_id_2 = #{user_id_1}) AND status = 1")
    Friendship selectFriendshipByIds(@Param("user_id_1")Integer user_id_1, @Param("user_id_2")Integer user_id_2);

    // 删除用户的某条双向好友关系记录（例如解除朋友关系，将状态设为 0 等操作）
    @Delete("UPDATE friendships SET status = 0 WHERE (user_id_1 = #{user_id_1} AND user_id_2 = #{user_id_2}) OR (user_id_1 = #{user_id_2} AND user_id_2 = #{user_id_1})")
    int deleteFriendshipByIds(@Param("user_id_1")Integer user_id_1, @Param("user_id_2")Integer user_id_2);

    // 查询两个用户之间是否存在双向好友关系（返回布尔值，更方便业务逻辑判断）
    @Select("SELECT COUNT(*) > 0 FROM friendships WHERE (user_id_1 = #{user_id_1} AND user_id_2 = #{user_id_2}) OR (user_id_1 = #{user_id_2} AND user_id_2 = #{user_id_1}) AND status = 1")
    boolean existsFriendshipByIds(@Param("user_id_1")Integer user_id_1, @Param("user_id_2")Integer user_id_2);
}